package com.sky.controller.user;

import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.ShoppingCart;
import com.sky.result.Result;
import com.sky.service.ShoppingCartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user/shoppingCart")
@Api(tags = "用户购物车相关接口")
@Slf4j
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 添加商品到购物车
     *
     * @param shoppingCartDTO 购物车数据传输对象
     * @return 操作结果
     */
    @PostMapping("/add")
    @ApiOperation("添加商品到购物车")
    public Result add(@RequestBody ShoppingCartDTO shoppingCartDTO) {
        log.info("添加商品到购物车: {}", shoppingCartDTO);

        shoppingCartService.add(shoppingCartDTO);

        return Result.success("商品已添加到购物车");
    }

    @PostMapping("/sub")
    public Result remove(@RequestBody ShoppingCartDTO shoppingCartDTO) {
        log.info("从购物车中移除商品: {}", shoppingCartDTO);

        shoppingCartService.sub(shoppingCartDTO);

        return Result.success("商品已从购物车中移除");
    }

    /**
     * 查询购物车列表
     *
     * @return 购物车数据传输对象列表
     */
    @GetMapping("/list")
    @ApiOperation("查询购物车列表")
    public Result<List<ShoppingCart>> list() {
        log.info("查询购物车列表");

        List<ShoppingCart> shoppingCartList = shoppingCartService.list();

        return Result.success(shoppingCartList);
    }

    /**
     * 清空购物车
     *
     * @return 操作结果
     */
    @DeleteMapping("/clean")
    @ApiOperation("清空购物车")
    public Result clear() {
        log.info("清空购物车");
        shoppingCartService.clean();
        return Result.success("购物车已清空");
    }
}
